﻿namespace Deppton.Mobile.Views
{
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows.Forms;
    using Deppton.Model;

    public partial class ChecksQuestionForm : QuestionForm
    {
        public ChecksQuestionForm()
        {
            this.OptionControls = new Dictionary<QuestionOption, CheckBox>();

            this.InitializeComponent();

            var options = new BindingList<QuestionOption>();
            this.Options = options;

            options.AddingNew += new AddingNewEventHandler(this.OptionsAddingNew);
            options.ListChanged += new ListChangedEventHandler(this.OptionsChanged);
        }

        public override bool ControlsEnabled
        {
            get
            {
                return base.ControlsEnabled;
            }
            set
            {
                base.ControlsEnabled = value;

                foreach (var control in this.OptionControls)
                {
                    control.Value.Enabled = value;
                }
            }
        }

        public override string Value
        {
            get
            {
                var values = new List<string>();

                foreach (var optionsControlKeyPair in this.OptionControls)
                {
                    if (optionsControlKeyPair.Value.Checked)
                    {
                        values.Add(optionsControlKeyPair.Key.Key);
                    }
                }

                return Utilities.EscapeList(values.ToArray());
            }

            set
            {
                var values = value != null ? Utilities.UnescapeList(value) : new string[0];

                foreach (var optionsControlKeyPair in this.OptionControls)
                {
                    optionsControlKeyPair.Value.Checked = values.Contains(optionsControlKeyPair.Key.Key);
                }
            }
        }

        public ICollection<QuestionOption> Options
        {
            get;
            private set;
        }

        private Dictionary<QuestionOption, CheckBox> OptionControls
        {
            get;
            set;
        }

        private void OptionsAddingNew(object sender, AddingNewEventArgs e)
        {
            this.RefreshInformation();
        }

        private void OptionsChanged(object sender, ListChangedEventArgs e)
        {
            this.RefreshInformation();
        }

        private void RefreshInformation()
        {
            foreach (var option in new List<QuestionOption>(this.OptionControls.Keys))
            {
                var control = this.OptionControls[option];

                this.CheckOptionsPanel.Controls.Remove(control);

                this.OptionControls.Remove(option);
            }

            int nextLocation = 3;
            foreach (var option in this.Options)
            {
                var control = new CheckBox
                {
                    Location = new System.Drawing.Point(3, nextLocation),
                    Size = new System.Drawing.Size(this.CheckOptionsPanel.Width - 6, 20),
                    Text = option.Description,
                    Tag = option,
                    Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right,
                    Enabled = this.ControlsEnabled
                };

                this.OptionControls.Add(option, control);
                this.CheckOptionsPanel.Controls.Add(control);
                nextLocation += 23;
            }
        }
    }
}